package com.ktwlsoft.fundmanage.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ktwlsoft.fundmanage.domain.business.FundInService;
import com.ktwlsoft.fundmanage.domain.business.FundOutService;
import com.ktwlsoft.fundmanage.domain.entity.FundSuperviseAccept;
import com.ktwlsoft.fundmanage.domain.entity.TransferApply;
import com.ktwlsoft.fundmanage.domain.repository.FundSuperviseAcceptRepository;
import com.ktwlsoft.fundmanage.payload.NetSignResult;
import com.ktwlsoft.fundmanage.payload.fundIn.BusinessCancelRequest;
import com.ktwlsoft.fundmanage.payload.fundOut.TransactRequest;
import com.ktwlsoft.fundmanage.utils.serviceInteraction.NetSignInteraction;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class NetSignScanTask {

    @Autowired
    private NetSignInteraction netSignInteraction;

    @Autowired
    private FundSuperviseAcceptRepository fundSuperviseAcceptRepository;

    @Autowired
    private FundInService fundInService;

    @Autowired
    private FundOutService fundOutService;

    /**
     * 更新往前状态
     */
    @Scheduled(fixedRate = 10000)
    @Transactional(rollbackFor = Exception.class)
    public void updateNetSign() {
        List<FundSuperviseAccept> list = fundSuperviseAcceptRepository.findByAcceptStateNotContaining(FundSuperviseAccept.AcceptState.CANCEL);
        for (FundSuperviseAccept f : list) {
            if (f.isTransferState()) { //是否过户
                continue;
            }
            if (fundOutService.hasTransApply(f.getTsCode())) {  //拥有划款单
                continue;
            }
            if (fundInService.hasLoanIn(f.getTsCode())) { //是否拥有贷款
                continue;
            }
            if (fundInService.hasFundIn(f.getTsCode())) { //是否有除了贷款以外的资金录入
                if (fundInService.doFundInCheck(f.getTsCode())) {
                    CancelFundAcceptance(f);
                    // todo:退款
                    List<TransferApply> transferApplies = fundOutService.getTransferApplyRepository().findByTsCode(f.getTsCode());
                    List<TransactRequest> transactRequestList = new ArrayList<>();
                    for (TransferApply t : transferApplies) {
                        TransactRequest r = new TransactRequest();
                        BeanUtils.copyProperties(t,r);
                        transactRequestList.add(r);
                    }

                } else {
                    f.setDoLock(true); // 锁定
                }
            } else {
                CancelFundAcceptance(f);
            }
            fundSuperviseAcceptRepository.save(f);
        }
    }

    private void CancelFundAcceptance(FundSuperviseAccept f) {
        NetSignResult<String> result = netSignInteraction.getShhSignBySigningCode(f.getNetSignCode());
        if (result.getSuccess()) {
            JSONObject data = JSON.parseObject(result.getData());
            if (data.getInteger("signingStatus") == 40) {
                BusinessCancelRequest request = new BusinessCancelRequest();
                request.setTsCode(f.getTsCode());
                if (fundInService.canCancelCase(request)) {
                    fundInService.cancelBusiness(request);
                }
            }
            if (data.getInteger("recordState") == 20) {
                f.setRecordState(true);
                f.setReferenceState(FundSuperviseAccept.ReferenceState.REFERENCED);
                f.setReferenceCode(data.getString("recordCode"));
            } else {
                f.setRecordState(false);
                f.setReferenceState(FundSuperviseAccept.ReferenceState.UNREFERENCED);
            }
            f.setReferenceHandleUserName(data.getString("handleUserName"));
            f.setReferenceHandleUserId(data.getString("handleUserId"));
            f.setHandoutCode(data.getString("immovablesUnitNum"));

            if (!f.isTransferState() && !f.isRecordState()) {
                if (!fundInService.hasLoanIn(f.getTsCode())) {
                    if (!fundOutService.hasTransApply(f.getTsCode())) {
                        f.setContractAmount(data.getBigDecimal("contractAmount"));
                    }
                }
            }
        }
    }


}
